package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.payconfig.EntitySchoolPhoneInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.schoolphone.SchoolPhoneInfoVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoSchoolPhone;
import com.ocom.payconfig.service.SchoolPhoneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class SchoolPhoneServiceImpl extends ServiceImpl<IDaoSchoolPhone, EntitySchoolPhoneInfo> implements SchoolPhoneService {

    @Autowired
    private IDaoSchoolPhone iDaoSchoolPhone;

    @Autowired
    private SchoolPhoneService schoolPhoneService;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster= MasterSlaveEnum.SLAVE)
    public Result getSchoolPhone(Long comId,SchoolPhoneInfoVo schoolPhoneInfoVo) {
        if (ObjectUtil.isEmpty(schoolPhoneInfoVo.getPage())) {
            schoolPhoneInfoVo.setPage(1);
        }
        if (ObjectUtil.isEmpty(schoolPhoneInfoVo.getSize())) {
            schoolPhoneInfoVo.setPage(10);
        }

        PageHelper.startPage(schoolPhoneInfoVo.getPage(), schoolPhoneInfoVo.getSize());
        List<SchoolPhoneInfoVo> list = iDaoSchoolPhone.getSchoolPhoneList(schoolPhoneInfoVo);
        PageInfo<SchoolPhoneInfoVo> pageInfo = new PageInfo<>(list);

        if (ObjectUtil.isEmpty(pageInfo.getList())) {
            return ResultUtil.success();
        }

        if(ObjectUtil.isEmpty(schoolPhoneInfoVo.getPId())){
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", pageInfo.getTotal());
            map.put("list",pageInfo.getList());
            return ResultUtil.success(map);
        }
        SchoolPhoneInfoVo phoneInfoVo = pageInfo.getList().get(0);
        return ResultUtil.success(phoneInfoVo);

    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster=MasterSlaveEnum.MASTER)
    public Result modifySchoolPhone(Long comId,SchoolPhoneInfoVo schoolPhoneInfoVo) {

        LambdaUpdateWrapper<EntitySchoolPhoneInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(EntitySchoolPhoneInfo::getComId, schoolPhoneInfoVo.getComId())
                .eq(EntitySchoolPhoneInfo::getPId, schoolPhoneInfoVo.getPId());

        updateWrapper.set(ObjectUtil.isNotEmpty(schoolPhoneInfoVo.getPhoneInfo()), EntitySchoolPhoneInfo::getPhoneInfo, schoolPhoneInfoVo.getPhoneInfo())
                .set(EntitySchoolPhoneInfo::getUpdateTime, new Date())
                .set(ObjectUtil.isNotEmpty(schoolPhoneInfoVo.getUpdateUser()), EntitySchoolPhoneInfo::getUpdateUser, schoolPhoneInfoVo.getUpdateUser())
                .set(ObjectUtil.isNotEmpty(schoolPhoneInfoVo.getUseInfo()), EntitySchoolPhoneInfo::getUseInfo, schoolPhoneInfoVo.getUseInfo())
                .set(ObjectUtil.isNotEmpty(schoolPhoneInfoVo.getCustomerInfo()), EntitySchoolPhoneInfo::getCustomerInfo, schoolPhoneInfoVo.getCustomerInfo())
                .set(ObjectUtil.isNotEmpty(schoolPhoneInfoVo.getQrCodeUrl()), EntitySchoolPhoneInfo::getQrCodeUrl, schoolPhoneInfoVo.getQrCodeUrl())
                .set(ObjectUtil.isNotEmpty(schoolPhoneInfoVo.getRemark()), EntitySchoolPhoneInfo::getRemark, schoolPhoneInfoVo.getRemark());

        boolean flag = schoolPhoneService.update(updateWrapper);
        if (!flag) {
            EntitySchoolPhoneInfo entitySchoolPhoneInfo = new EntitySchoolPhoneInfo();
            entitySchoolPhoneInfo.setComId(schoolPhoneInfoVo.getComId());
            entitySchoolPhoneInfo.setPId(schoolPhoneInfoVo.getPId());
            entitySchoolPhoneInfo.setPhoneInfo(schoolPhoneInfoVo.getPhoneInfo());
            entitySchoolPhoneInfo.setUseInfo(schoolPhoneInfoVo.getUseInfo());
            entitySchoolPhoneInfo.setCustomerInfo(schoolPhoneInfoVo.getCustomerInfo());
            entitySchoolPhoneInfo.setQrCodeUrl(schoolPhoneInfoVo.getQrCodeUrl());
            entitySchoolPhoneInfo.setRemark(schoolPhoneInfoVo.getRemark());
            entitySchoolPhoneInfo.setUpdateUser(schoolPhoneInfoVo.getUpdateUser());
            entitySchoolPhoneInfo.setUpdateTime(new Date());

            try {
                schoolPhoneService.save(entitySchoolPhoneInfo);
            } catch (Exception e) {
                log.info("异常信息{}",e.getMessage());
                return ResultUtil.error(ResultEnum.Err_2016);

            }
        }
        return ResultUtil.success();
    }
}
